#!/bin/bash

#
# FIXME
#
# The ordering of object files on the command-line influences the order
# in which library initializers run. We exploit this to ensure gadget's
# entrypoint runs *after* V8's static initializer has run.
#
# This is obviously a terrible hack, but will have to do until we find
# a better solution.
#

initializer_index=
initializer_object=

output_type=unknown

argv=()
while [[ $# > 0 ]]; do
  arg="$1"

  case "$arg" in
    lib/gadget/*/gadget-entrypoint-unix.c.o)
      initializer_index=${#argv[@]}
      initializer_object="$1"
      ;;
    -shared)
      output_type=shlib
      ;;
  esac

  argv+=("$arg")
  shift
done

if [[ -n "$initializer_object" && $output_type = shlib ]]; then
  unset argv[$initializer_index]
  argv+=("$initializer_object")
fi

exec "@driver@" "${argv[@]}"
